//
//  ToastView.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/25.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "ToastView.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

@interface ToastView ()
@property (nonatomic, retain) UIView *showView;
@property (nonatomic, retain) UILabel *promptLabel;
@end

@implementation ToastView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    NSLog(@"%@ dealloc..",NSStringFromClass([self class]));
}

- (instancetype)init {
    self = [super init];
    
    if (self) {
        //透明蒙版
        self.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT);
        self.backgroundColor = [UIColor clearColor];
        
        //显示提示的框
        self.showView = [[UIView alloc] initWithFrame:CGRectZero];
        self.showView.backgroundColor = RGBA(0, 0, 0, 0.65);
        [self.showView.layer setCornerRadius:5.0f];
        [self.showView setClipsToBounds:YES];
        [self addSubview:self.showView];
        
        self.promptLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.promptLabel.textAlignment = NSTextAlignmentCenter;
        self.promptLabel.textColor = [UIColor whiteColor];
        self.promptLabel.font = [UIFont systemFontOfSize:16.0f];
        self.promptLabel.text = @"默认提示";
        self.promptLabel.numberOfLines = 0;
        [self.showView addSubview:self.promptLabel];
        
        self.showView.width = self.width * 0.618;
        self.showView.centerY = self.height/2;
        self.showView.centerX = self.width/2;
        self.promptLabel.width = self.showView.width - 15.0f * 2;
        
        _duration = 2.0f;//默认2秒
    }
    return self;
}

#pragma mark - Public Api

+ (void)showMsg:(NSString *)msg {
    [self showMsg:msg inView:nil withDuration:2.0f dismissBlock:nil];
}

+ (void)showMsg:(NSString *)msg inView:(UIView *)view {
    [self showMsg:msg inView:view withDuration:2.0f dismissBlock:nil];
}

+ (void)showMsg:(NSString *)msg inView:(UIView *)view withDuration:(NSTimeInterval)duration {
    [self showMsg:msg inView:view withDuration:duration dismissBlock:nil];
}

+ (void)showMsg:(NSString *)msg inView:(UIView *)view dismissBlock:(ToastDismissBlock)block {
    [self showMsg:msg inView:view withDuration:2.0f dismissBlock:block];
}

+ (void)showMsg:(NSString *)msg inView:(UIView *)view withDuration:(NSTimeInterval)duration dismissBlock:(ToastDismissBlock)block {
    if (msg == nil || ![msg isKindOfClass:[NSString class]]) {
        return;
    }
    ToastView *toastView = [[ToastView alloc] init];
    toastView.duration = duration;
    toastView.block = block;
    toastView.promptMsg = msg;
    
    [toastView showInView:view];
}

#pragma mark - Private Api

- (void)setPromptMsg:(NSString *)promptMsg {
    _promptMsg = promptMsg;
    
    [self fitToPromptMsg:_promptMsg];
}

- (void)fitToPromptMsg:(NSString *)promptMsg {
    
    self.promptLabel.text = promptMsg?:@"";
    //长度特殊处理
    CGFloat maxLabelWith = self.promptLabel.width;
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.promptLabel.font,NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil];
    CGRect rect = [self.promptLabel.text boundingRectWithSize:CGSizeMake(maxLabelWith, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    CGSize size = rect.size;
    
    CGSize fitSize = [self.promptLabel sizeThatFits:size];
    
    self.promptLabel.height = fitSize.height;
    self.promptLabel.width = fitSize.width;
    
    self.showView.height = self.promptLabel.height + 17.0f*2;
    self.showView.centerY = self.height/2;
    
    self.promptLabel.centerX = self.showView.width/2;
    self.promptLabel.centerY = self.showView.height/2;
}

- (void)showInView:(UIView *)view {
    if (!view) {
        UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
        if ([window.subviews containsObject:self]) {
            [self dismissPromptView];
        }
        
        [window addSubview:self];
        
    } else {
        if ([view.subviews containsObject:self]) {
            [self dismissPromptView];
        }
        
        self.showView.centerY = view.height/2 -32;
        [view addSubview:self];
    }
    
    if (_block) {
        self.userInteractionEnabled = YES; //弹出提示宽后不可操作
    } else {
        self.userInteractionEnabled = NO;  //弹出提示宽后可操作
    }
    
    [self showAnimate];
    
    if (_duration > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissPromptView];
        });
    } else {
        [self dismissPromptView];
    }
}

- (void)showAnimate {
    self.showView.transform = CGAffineTransformScale(self.showView.transform, 1.2, 1.2);
    [UIView animateWithDuration:0.15
                          delay:0
                        options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         self.showView.transform = CGAffineTransformScale(self.showView.transform, 1/1.2, 1/1.2);
                     }
                     completion:NULL];
}

- (void)dismissPromptView {
    [UIView animateWithDuration:0.25f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        if (_duration > 0) {
            if (_block) {
                _block();
            }
        }
        [self removeFromSuperview];
    }];
}

@end
